﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Components.FileWatcher;

namespace Ue.Framework.Tests.Components.FileWatcher
{
    /// <summary>
    /// FileMonitorManager 的摘要说明
    /// </summary>
    [TestClass]
    public class FileMonitorManagerTests
    {
        private IFileMonitorManager fileMonitorManager;
        private IFileMonitor fileMonitor;

        [TestInitialize]
        public void Initial()
        {
            this.fileMonitorManager = new FileMonitorManager();
        }

        [TestMethod]
        [DeploymentItem("Components/FileWatcher/File.txt")]
        public void IFileMonitorEquals()
        {
            var directoryPath=System.Environment.CurrentDirectory;
            var filter="File.txt";
            var changeTypes= WatcherChangeTypes.Changed | WatcherChangeTypes.Created;
            var isContinued=true;

            var lable = this.fileMonitorManager.TryAcquireMonitor(
                directoryPath, 
                filter, 
                isContinued, 
                ctx => { },
                changeTypes, 
                ref this.fileMonitor);

            Assert.IsTrue(lable);

            Assert.AreEqual(directoryPath, this.fileMonitor.DirectoryPath);
            Assert.AreEqual(filter,this.fileMonitor.Filter);
            Assert.AreEqual(changeTypes,this.fileMonitor.ActionTypes);
            Assert.IsTrue(this.fileMonitor.IsContinued);
        }

        [TestMethod]
        [DeploymentItem("Components/FileWatcher")]
        public void FileWatch()
        {
            var changed = 0;
            this.fileMonitorManager.TryAcquireMonitor(
                System.Environment.CurrentDirectory, 
                "*.txt", 
                true, 
                ctx =>{
                    ++changed;
                }, 
                System.IO.WatcherChangeTypes.All,
                ref this.fileMonitor);

            Action<int> action = changedCount =>
            {
                using (StreamWriter writer = new StreamWriter("File.txt"))
                {
                    writer.Write(1);
                }
                using (StreamWriter writer = new StreamWriter("File2.txt"))
                {
                    writer.Write(1);
                }
                System.Threading.Thread.Sleep(600);
                Assert.AreEqual(changedCount, changed);
            };

            Assert.AreEqual(0, changed);

            action(2);

            action(4);

            action(6);
        }

        [TestMethod]
        [DeploymentItem("Components/FileWatcher")]
        public void OneFileWatchByFilter()
        {
            var changed = 0;
            this.fileMonitorManager.TryAcquireMonitor(System.Environment.CurrentDirectory, "File.txt", true, ctx => ++changed, System.IO.WatcherChangeTypes.All, ref this.fileMonitor);

            Action<int> action = changedCount =>
            {
                using (StreamWriter writer = new StreamWriter("File.txt"))
                {
                    writer.Write(1);
                }
                using (StreamWriter writer = new StreamWriter("File2.txt"))
                {
                    writer.Write(1);
                }
                System.Threading.Thread.Sleep(600);
                Assert.AreEqual(changedCount, changed);
            };

            Assert.AreEqual(0, changed);

            action(1);

            action(2);

            action(3);
        }

        [TestMethod]
        public void CreateChange()
        {
            var changed = 0;

            this.fileMonitorManager.TryAcquireMonitor(System.Environment.CurrentDirectory, "*.*", true, ctx => ++changed, System.IO.WatcherChangeTypes.Created, ref this.fileMonitor);

            Action<int> action = changedCount =>
            {
                using (FileStream stream = File.Create(string.Format("File1-{0}.txt",changedCount))) { }

                System.Threading.Thread.Sleep(600);
                Assert.AreEqual(changedCount, changed);
            };

            Assert.AreEqual(0, changed);

            action(1);

            action(2);
        }

        [TestMethod]
        public void NotContinued()
        {
            var changed = 0;

            this.fileMonitorManager.TryAcquireMonitor(System.Environment.CurrentDirectory, "*.*", false, ctx => ++changed, System.IO.WatcherChangeTypes.Created, ref this.fileMonitor);

            Action<int> action = changedCount =>
            {
                using (FileStream stream = File.Create(string.Format("File2-{0}.txt", changedCount))) { }

                System.Threading.Thread.Sleep(600);
                Assert.AreEqual(changedCount, changed);
            };

            Assert.AreEqual(0, changed);

            action(1);

            action(1);

            action(1);
        }
    }
}
